package com.hxcoder.ucenter.controller;

import com.hxcoder.commonutils.JwtUtils;
import com.hxcoder.commonutils.R;
import com.hxcoder.ucenter.entity.UcenterMember;
import com.hxcoder.ucenter.entity.vo.RegisterVo;
import com.hxcoder.ucenter.service.UcenterMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author hxcoder
 */
@Api(tags = "会员管理")
@RestController
@RequestMapping("/ucenter/member")
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember member) {

        String token = memberService.login(member);
        return R.ok().data("token", token);
    }

    @ApiOperation(value = "会员注册")
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo) {

        memberService.register(registerVo);
        return R.ok();
    }

    @ApiOperation(value = "获取用户信息", notes = "根据token获取用户信息")
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request) {

        //根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据用户id获取用户信息
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("userInfo", member);
    }

    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day) {
        Integer count = memberService.countRegister(day);
        return R.ok().data("countRegister", count);
    }
}